<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Manage | 基础设置</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Pace -->
    <link rel="stylesheet" href="../../plugins/AdminLTE/plugins/pace-master/themes/blue/pace-theme-flash.css">
    <script type="text/javascript" src="../../plugins/AdminLTE/plugins/pace-master/pace.min.js"></script>
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="../../plugins/AdminLTE/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="../../plugins/AdminLTE/dist/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="../../plugins/AdminLTE/dist/css/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="../../plugins/AdminLTE/dist/css/AdminLTE.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="../../plugins/AdminLTE/plugins/iCheck/all.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>
        基础设置
    </h1>
    <ol class="breadcrumb">
        <li><i class="fa fa-home"></i> Home</li>
        <li>系统设置</li>
        <li>基础设置</li>
    </ol>
</section>

<section class="content">

    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title">登录设置
                <small>超级管理员不受以下规则限制</small>
            </h3>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" data-widget="collapse">
                    <i class="fa fa-minus"></i>
                </button>
            </div>
        </div>
        <div class="box-body">
            <form id="config_login_rule" onsubmit="return false" class="form-horizontal col-xs-12">
                <div class="form-group col-xs-12">
                    <label class="col-sm-3 control-label">是否开启单用户在线数校验</label>
                    <div class="input-group col-sm-9" style="margin-top: 5px;">
                        <label>
                            <input type="radio" name="saovalidate" value="true">开启
                        </label>
                        <label style="margin-left: 10px">
                            <input type="radio" name="saovalidate" value="false" checked>关闭
                        </label>
                        <span class="help-block ">如果开启，则用户登录时会检测该账号的在线数，当该账号在线数已达到配置数，则该账号不可在其他设备登录！</span>
                    </div>
                </div>
                <div class="form-group col-xs-12">
                    <label class="col-sm-3 control-label">单个账号最多可在线数</label>
                    <div class="input-group col-sm-9">
                        <input type="number" min="1" name="saonumber" class="form-control">
                    </div>
                </div>
                <div class="form-group col-xs-12">
                    <label class="col-sm-3 control-label">是否启用登录错误次数验证</label>
                    <div class="input-group col-sm-9" style="margin-top: 5px;">
                        <label>
                            <input type="radio" name="lenvalidate" value="true">开启
                        </label>
                        <label style="margin-left: 10px">
                            <input type="radio" name="lenvalidate" value="false" checked>关闭
                        </label>
                        <span class="help-block ">如果开启，则用户登录失败时会进行校验，当连续失败次数达到配置数时，该账号在配置时间内不可再进行登录！</span>
                    </div>
                </div>
                <div class="form-group col-xs-12">
                    <label class="col-sm-3 control-label">错误多少次后锁定账号</label>
                    <div class="input-group col-sm-9">
                        <input type="number" min="1" name="errornumber" class="form-control">
                    </div>
                </div>
                <div class="form-group col-xs-12">
                    <label class="col-sm-3 control-label">锁定账号时间</label>
                    <div class="input-group col-sm-9">
                        <input type="number" min="1" name="againlogin" class="form-control">
                        <div class="input-group-addon">分钟</div>
                    </div>
                </div>
            </form>
        </div>
        <div id="login_overlay" class="overlay">
            <i class="fa fa-refresh fa-spin"></i>
        </div>
        <div class="box-footer text-center">
            <button onclick="changeLoginRule()" type="button" class="btn btn-primary">更新</button>
        </div>
    </div>

    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title">邮件服务
                <small>邮件服务异常时，系统中涉及邮件发送的相关功能将不可用</small>
            </h3>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" data-widget="collapse">
                    <i class="fa fa-minus"></i>
                </button>
            </div>
        </div>
        <div class="box-body">
            <form id="config_email_server" onsubmit="return false" class="form-horizontal col-xs-12">
                <div class="form-group col-xs-12">
                    <label class="col-sm-3 control-label">服务器地址</label>
                    <div class="input-group col-sm-9">
                        <input type="text" name="host" class="form-control">
                    </div>
                </div>
                <div class="form-group col-xs-12">
                    <label class="col-sm-3 control-label">端口</label>
                    <div class="input-group col-sm-9">
                        <input type="text" name="port" class="form-control">
                    </div>
                </div>
                <div class="form-group col-xs-12">
                    <label class="col-sm-3 control-label">账号</label>
                    <div class="input-group col-sm-9">
                        <input type="text" name="username" class="form-control">
                    </div>
                </div>
                <div class="form-group col-xs-12">
                    <label class="col-sm-3 control-label">授权码</label>
                    <div class="input-group col-sm-9">
                        <input type="password" name="password" class="form-control">
                    </div>
                </div>
            </form>
        </div>
        <div id="email_overlay" class="overlay">
            <i class="fa fa-refresh fa-spin"></i>
        </div>
        <div class="box-footer text-center">
            <button onclick="testConnection()" type="button" class="btn btn-info">测试</button>
            <button onclick="changeEmailServer()" type="button" class="btn btn-primary" style="margin-left: 20px">更新
            </button>
        </div>
    </div>
</section>
<!-- jQuery 2.2.3 -->
<script src="../../plugins/AdminLTE/plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- jQuery UI -->
<script src="../../plugins/AdminLTE/plugins/jQueryUI/jquery-ui.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="../../plugins/AdminLTE/bootstrap/js/bootstrap.min.js"></script>
<!-- FastClick -->
<script src="../../plugins/AdminLTE/plugins/fastclick/fastclick.js"></script>
<!-- iCheck -->
<script src="../../plugins/AdminLTE/plugins/iCheck/icheck.js"></script>
<!-- AdminLTE App -->
<script src="../../plugins/AdminLTE/dist/js/app.min.js"></script>
<script>
    $.fn.serializeJson = function () {
        var serializeObj = {};
        var array = this.serializeArray();
        var str = this.serialize();
        $(array).each(function () {
            if (serializeObj[this.name]) {
                if ($.isArray(serializeObj[this.name])) {
                    serializeObj[this.name].push(this.value);
                } else {
                    serializeObj[this.name] = [serializeObj[this.name], this.value];
                }
            } else {
                serializeObj[this.name] = this.value;
            }
        });
        return serializeObj;
    };
    $(function () {
        $(':radio').iCheck({
            checkboxClass: 'icheckbox_flat-green',
            radioClass: 'iradio_flat-green'
        });
        loadSystemConfig();
    })

    function changeEmailServer() {
        if (!$("input[name='host']").val() || !$("input[name='port']").val() || !$("input[name='username']").val() || !$("input[name='password']").val()) {
            parent.toastr.error("请填写完整表单");
            return false;
        }
        $("#email_overlay").show();
        $.ajax({
            type: "post",
            data: {
                jsondata: JSON.stringify($("#config_email_server").serializeJson())
            },
            url: "/system/updateEmailServerConfig",
            success: function (result) {
                $("#email_overlay").hide();
                if (result.success) {
                    parent.toastr.success("邮件服务配置更新成功")
                } else {
                    parent.toastr.error(result.message);
                }
            }
        });
    }

    function testConnection() {
        $("#email_overlay").show();
        $.ajax({
            type: "post",
            data: $("#config_email_server").serialize(),
            url: "/system/testConnection",
            success: function (result) {
                $("#email_overlay").hide();
                if (result.message == 'success') {
                    parent.toastr.success("邮件服务可用")
                } else {
                    parent.toastr.error(result.message);
                }
            }
        });
    }

    function changeLoginRule() {
        if (!$("input[name='saonumber']").val() || !$("input[name='errornumber']").val() || !$("input[name='againlogin']").val()) {
            parent.toastr.error("请填写完整表单");
            return false;
        }
        if ($("input[name='saonumber']").val() <= 0 || $("input[name='errornumber']").val() <= 0 || $("input[name='againlogin']").val() <= 0) {
            parent.toastr.error("请填写大于0的值");
            return false;
        }
        $("#login_overlay").show();
        $.ajax({
            type: "post",
            data: {
                jsondata: JSON.stringify($("#config_login_rule").serializeJson())
            },
            url: "/system/updateLoginRuleConfig",
            success: function (result) {
                $("#login_overlay").hide();
                if (result.success) {
                    parent.toastr.success("登录规则更新成功")
                } else {
                    parent.toastr.error(result.message);
                }
            }
        });
    }

    function loginRule(data) {
        data = JSON.parse(data);
        $("input[name='saovalidate'][value='" + data.saovalidate + "']").iCheck('check');
        $("input[name='saonumber']").val(data.saonumber);
        $("input[name='lenvalidate'][value='" + data.lenvalidate + "']").iCheck('check');
        $("input[name='errornumber']").val(data.errornumber);
        $("input[name='againlogin']").val(data.againlogin);
    }

    function emailServer(data) {
        data = JSON.parse(data);
        $("input[name='host']").val(data.host);
        $("input[name='port']").val(data.port);
        $("input[name='username']").val(data.username);
        $("input[name='password']").val(data.password);
    }

    function loadSystemConfig() {
        $.ajax({
            type: "get",
            url: "/system/getConfigs",
            success: function (result) {
                $("#login_overlay").hide();
                $("#email_overlay").hide();
                if (result.success) {
                    loginRule(result.data['LOGIN_RULE']);
                    emailServer(result.data['EMAIL_SERVER']);
                } else {
                    parent.toastr.error(result.message);
                }
            }
        });
    }
</script>
</body>
</html>
